#include "mainwindow.h"
#include "ui_mainwindow.h"

const QString BTN_color("background-color: red;");
const QString BTN_color2("background-color: green;");

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //连接设备
    QGamepad *m_gamepad = new QGamepad(0, this);


    connect(m_gamepad, &QGamepad::buttonL3Changed, this, [this](bool pressed){
        switch (pressed) {
        case true:
            this->ui->BT_Cent_L->setStyleSheet(BTN_color);
            //qDebug() << "Button Atrue:" ;
            break;
        case false:
            this->ui->BT_Cent_L->setStyleSheet(BTN_color2);
            //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button B" << pressed;
    });


    connect(m_gamepad, &QGamepad::buttonR3Changed, this, [this](bool pressed){
        switch (pressed) {
        case true:
            this->ui->BT_Cent_R->setStyleSheet(BTN_color);
            //qDebug() << "Button Atrue:" ;
            break;
        case false:
            this->ui->BT_Cent_R->setStyleSheet(BTN_color2);
            //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button B" << pressed;
    });


    connect(m_gamepad, &QGamepad::axisLeftXChanged, this, [this](double value){
        emit changeValue1(value*100);
        qDebug() << "Left X" << value;
    });
    connect(m_gamepad, &QGamepad::axisLeftYChanged, this, [this](double value){
        emit changeValue2(value*100);
        qDebug() << "Left Y" << value;
    });
    connect(m_gamepad, &QGamepad::axisRightXChanged, this, [this](double value){
        emit changeValue5(value*100);
        qDebug() << "Right X" << value;
    });
    connect(m_gamepad, &QGamepad::axisRightYChanged, this, [this](double value){
        emit changeValue4(value*100);
        qDebug() << "Right Y" << value;
    });
    connect(m_gamepad, &QGamepad::buttonL2Changed, this, [this](double value){
         emit changeValue_L(value*100);
        qDebug() << "Button L2: " << value;
    });
    connect(m_gamepad, &QGamepad::buttonR2Changed, this, [this](double value){
         emit changeValue_R(value*100);
        qDebug() << "Button R2: " << value;
    });
    connect(m_gamepad, &QGamepad::buttonAChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_A->setStyleSheet(BTN_color);
                qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_A->setStyleSheet(BTN_color2);
                qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button A" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonBChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_B->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_B->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button B" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonXChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_X->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_X->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button X" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonYChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_Y->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_Y->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Y" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonL1Changed, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_L1->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_L1->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button L1" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonR1Changed, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_r1->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_r1->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button R1" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonSelectChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_SELECT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_SELECT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Select" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonStartChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_START->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_START->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "Button Start" << pressed;
    });

    connect(m_gamepad, &QGamepad::buttonGuideChanged, this, [](bool pressed){
        qDebug() << "Button Guide" << pressed;
    });

    connect(m_gamepad, &QGamepad::buttonUpChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_UP->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_UP->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonUp" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonDownChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_DOWN->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_DOWN->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonDown" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonLeftChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_LEFT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_LEFT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonLeft" << pressed;
    });
    connect(m_gamepad, &QGamepad::buttonRightChanged, this, [this](bool pressed){
        switch (pressed) {
            case true:
                this->ui->BT_RIGHT->setStyleSheet(BTN_color);
                //qDebug() << "Button Atrue:" ;
                break;
            case false:
                this->ui->BT_RIGHT->setStyleSheet(BTN_color2);
                //qDebug() << "Button Afalse:" ;
            break;
        }
        //qDebug() << "buttonRight" << pressed;
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeValue1(double value)
{
    ui->progressBar_1->setValue(value);
}

void MainWindow::changeValue2(double value)
{
    ui->progressBar_2->setValue(value);
}

void MainWindow::changeValue4(double value)
{
    ui->progressBar_4->setValue(value);
}

void MainWindow::changeValue5(double value)
{
    ui->progressBar_5->setValue(value);
}

void MainWindow::changeValue_R(double value)
{
    ui->progressBar_3->setValue(value);
}


void MainWindow::changeValue_L(double value)
{
    ui->progressBar_6->setValue(value);
}

